//
//  YAYIPerformanceViewController.m
//  YAYIDoctor
//
//  Created by Veer on 16/5/9.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIPerformanceViewController.h"
#import "YAYITodayViewController.h"
#import "YAYIThisMonthViewController.h"
#import "YAYIThisYearViewController.h"

@interface YAYIPerformanceViewController () <UIScrollViewDelegate>



@property (nonatomic, weak) UIView *topView;
@property (nonatomic, weak) UIView *indicatorView;
@property (nonatomic, weak) UIScrollView *scrollView;

@property (nonatomic, assign) NSInteger curSelectedIndex;

@end

@implementation YAYIPerformanceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的业绩";
    self.view.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    
     YAYILog(@"self.view.frame:%@", NSStringFromCGRect(self.view.frame));
    
//    [self setupNav];
    
    [self setupTopView];
    [self setupScrollView];
    [self addChildVcs];

    
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    UINavigationBar *bar = self.navigationController.navigationBar;
    [bar setBackgroundImage:[UIImage imageNamed:@"clear_bg"] forBarMetrics:UIBarMetricsDefault];
    [bar setShadowImage:[UIImage new]];
    
    bar.backgroundColor = [UIColor clearColor];
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    UINavigationBar *bar = self.navigationController.navigationBar;
    [bar setBackgroundImage:[UIImage imageNamed:@"navagation_bg"] forBarMetrics:UIBarMetricsDefault];
}


- (void)setupNav
{
    //    UINavigationBar *bar = [UINavigationBar appearanceWhenContainedIn:[self class], nil];
    UINavigationBar *bar = self.navigationController.navigationBar;
    [bar setBackgroundImage:[UIImage imageNamed:@"clear_bg"] forBarMetrics:UIBarMetricsDefault];
    [bar setShadowImage:[UIImage new]];
    
    bar.backgroundColor = [UIColor clearColor];
}


- (void)setupTopView
{
//    self.view.y -= 64;
    
    UIImageView *topImgBg = [[UIImageView alloc] init];
    topImgBg.backgroundColor = [UIColor yellowColor];
    topImgBg.image = [UIImage imageNamed:@"navagation_bg"];
    topImgBg.frame = CGRectMake(0, 0, SCREEN_width, 64 + 40 + 100);
    [self.view addSubview:topImgBg];
    
    UIView *topView = [[UIView alloc] init];
//    topView.backgroundColor = YAYIColor(250, 250, 251);
    [self.view addSubview:topView];
    self.topView = topView;
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(64);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@40);
    }];
    
    
    NSArray *titleArray = @[@"今日", @"本月", @"本年"];
    
    CGFloat titleBtnW = SCREEN_width / titleArray.count;
    for (int i = 0; i < 3; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [btn setTitle:titleArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        [btn setTitleColor:YAYIColor(83, 165, 243) forState:UIControlStateSelected];
        if (i == 0) {
            btn.selected = YES;
        }
        [topView addSubview:btn];
        [btn addTarget:self action:@selector(topViewAction:) forControlEvents:UIControlEventTouchUpInside];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(titleBtnW));
            make.left.equalTo(@(i * titleBtnW));
            make.top.equalTo(@0);
            make.height.equalTo(topView);
        }];
    }
    
    
//    // 线
//    UIView *lineView = [[UIView alloc] init];
//    lineView.backgroundColor = YAYIColor(0xdd, 0xdd, 0xdd);
//    [topView addSubview:lineView];
//    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.equalTo(@0.5);
//        make.left.right.equalTo(topView);
//        make.bottom.equalTo(topView);
//    }];
    
    // 初始化底标示
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [UIColor whiteColor];
    [topView addSubview:indicatorView];
    self.indicatorView = indicatorView;
    [indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.bottom.mas_equalTo(topView.mas_bottom);
        make.width.mas_equalTo(titleBtnW - 30);
        make.height.mas_equalTo(2);
    }];
    
    self.curSelectedIndex = 0;
}


- (void)setupScrollView
{

    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.pagingEnabled = YES;
//    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.delegate = self;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.topView.mas_bottom);
    }];
    
    scrollView.contentSize = CGSizeMake(SCREEN_width * 3, 0);
    
}


- (void)addChildVcs
{
    YAYITodayViewController *vc1 = [[YAYITodayViewController alloc] init];
    [self addChildViewController:vc1];
    vc1.view.frame = self.scrollView.bounds;
    vc1.view.x = 0;
    [self.scrollView addSubview:vc1.view];
    
    YAYIThisMonthViewController *vc2 = [[YAYIThisMonthViewController alloc] init];
    [self addChildViewController:vc2];
    vc2.view.frame = self.scrollView.bounds;
    vc2.view.x = SCREEN_width * 1.0;
    [self.scrollView addSubview:vc2.view];
    
    YAYIThisYearViewController *vc3 = [[YAYIThisYearViewController alloc] init];
    [self addChildViewController:vc3];
    vc3.view.frame = self.scrollView.bounds;
    vc3.view.x = SCREEN_width * 2.0;
    [self.scrollView addSubview:vc3.view];
    
}


#pragma mark - 点击事件
- (void)topViewAction:(UIButton *)sender
{
    if (sender.selected == YES) {
        return;
    }
    NSInteger index = sender.tag;
    UIButton *lastBtn = self.topView.subviews[self.curSelectedIndex];
    lastBtn.selected = NO;
    sender.selected = YES;
    self.curSelectedIndex = index;
    
    [self.scrollView setContentOffset:CGPointMake(index * SCREEN_width, 0) animated:YES];
}


#pragma mark - 代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat with = SCREEN_width / 3.0;
    //    NSInteger count = scrollView.contentOffset.x / SCREEN_width;
    CGFloat w = scrollView.contentOffset.x * (with / SCREEN_width);
    self.indicatorView.x = w + 15;
    
}


/** 滚动结束（手势导致） */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}



- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / SCREEN_width;
    
    
    if (1 == index) {
        YAYIThisMonthViewController *vc = self.childViewControllers[index];
        if (!vc.isHadFreshed) {
            [vc getPerformance];
        }
    }else if (2 == index){
        YAYIThisYearViewController *vc = self.childViewControllers[index];
        if (!vc.isHadFreshed) {
            [vc getPerformance];
        }
    }
    
    
    UIButton *btn = self.topView.subviews[index];
    UIButton *lastBtn = self.topView.subviews[self.curSelectedIndex];
    lastBtn.selected = NO;
    btn.selected = YES;
    
    self.curSelectedIndex = index;
}
@end
